<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>拓深科技</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link href="assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="assets/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <link href="assets/ionicons-2.0.1/css/ionicons.min.css" rel="stylesheet"/>
    <link href="assets/plugins/select2/select2.min.css" rel="stylesheet"/>
    <link href="assets/AdminLTE/dist/css/AdminLTE.min.css" rel="stylesheet"/>
    <link href="assets/AdminLTE/dist/css/skins/_all-skins.min.css" rel="stylesheet"/>
    <link href="assets/DataTables-1.10.11/media/css/dataTables.bootstrap.min.css" rel="stylesheet"/>
    <link href="assets/images/log.ico" rel="shortcut icon">
    <link href="assets/ipicture/css/iPicture_location.css" rel="stylesheet" media="screen"/>
    <style>
        .table>tbody>tr>td{
            text-align:center;
        }
        .table>thead:first-child>tr:first-child>th{
            text-align:center;
        }
    </style>
</head>
<body class="hold-transition skin-red sidebar-mini">
<div class="wrapper">
    <!--顶部导航栏开始-->
    <header id="top_header" class="main-header">
        <!-- Logo -->
        <a href="main_page.html" class="logo">
            <!-- mini logo for sidebar mini 50x50 pixels -->
            <span class="logo-mini"><b>T</b>pson</span>
            <!-- logo for regular state and mobile devices -->
            <span class="logo-lg"><b>T</b>PSON</span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top" role="navigation">
            <!-- Sidebar toggle button-->
            <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <div class="navbar-custom-menu">
                <ul class="nav navbar-nav">

                    <!-- User Account: style can be found in dropdown.less -->
                    <li class="dropdown user user-menu">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <!--<img src="assets/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">-->
                            <span class="hidden-xs">Administrator</span>
                        </a>
                    </li>
                    <!-- Control Sidebar Toggle Button -->
                </ul>
            </div>
        </nav>
    </header>
    <!--顶部导航栏结束-->
    <!--右侧导航栏开始-->
    <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
            <!-- Sidebar user panel -->
            <!-- sidebar menu: : style can be found in sidebar.less -->
            <ul id="side_bar" class="sidebar-menu">
                <li class="header">导航栏</li>
                <li class="treeview">
                    <a href="main_page.html">
                        <i class="fa fa-dashboard text-red"></i>
                        <span>主页</span>
                    </a>
                </li>

                <li class="treeview">
                    <a href="alert_log.html">
                        <i class="fa fa-warning text-red"></i>
                        <span>报警记录</span>
                    </a>
                </li>
                <li class="treeview">
                    <a href="map_show.html">
                        <i class="fa fa-map-marker text-red"></i>
                        <span>地图展示</span>
                    </a>
                </li>
                <li class="treeview active">
                    <a href="#">
                        <i class="fa fa-cog text-red"></i>
                        <span>设备管理</span>
                        <i class="fa fa-angle-left pull-right"></i>
                    </a>
                    <ul class="treeview-menu">
                        <li><a href="router_config.html"><i class="fa fa-circle-o text-red"></i>设备配置 </a> </li>
                    </ul>
                </li>
                <li class="treeview">
                    <a href="#">
                        <i class="fa fa-chain-broken text-red"></i>
                        <span>监测点配置</span>
                        <i class="fa fa-angle-left pull-right"></i>
                    </a>
                    <ul class="treeview-menu">
                        <li><a href="monitor_config_map_config.html"><i class="fa fa-circle-o text-yellow"></i>地图配置 </a> </li>
                        <li><a href="monitor_config_sensor_type_input.html"><i class="fa fa-circle-o text-yellow"></i>传感器类别设置 </a> </li>
                        <li><a href="monitor_config_monitor_input.html"><i class="fa fa-circle-o text-yellow"></i>监测点配置 </a> </li>
                        <li><a href="monitor_config_export.html"><i class="fa fa-circle-o text-yellow"></i>数据导入导出 </a> </li>
                    </ul>
                </li>

                <li id="sys_log" class="treeview">
                    <a href="show_log.html">
                        <i class="fa fa-files-o text-red"></i>
                        <span>系统日志</span>
                    </a>
                </li>
            </ul>
        </section>
        <!-- /.sidebar -->
    </aside>
    <!--右侧导航栏结束-->
    <!--内容区开始-->
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                <b>设备配置</b>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-cog"></i> 设备管理</a> </li>
                <li><a href="router_config.html"><i class="fa fa-circle-o"></i> 设备配置</a></li>
            </ol>
        </section>
        <section class="invoice">
            <div class="row">
                <div class="box box-success">
                    <div class="box-header">
                        <h4 class="box-title"><b>录入用户信息传输装置</b></h4>
                    </div>
                    <div class="box-body">
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label>设备名称</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-sitemap"></i></span>
                                    <input id="eqName" type="text" class="form-control" placeholder="请输入设备名称">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label>设备IP</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-plug"></i></span>
                                    <input id="eqIP" type="text" class="form-control" placeholder="请输入设备IP">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label>设备备注</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-file-text"></i></span>
                                    <input id="eqDes" type="text" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <button id="saveEqBtn" type="button" class="btn btn-success btn-block pull-right" style="margin-top: 25px"><i class="fa  fa-check-square-o"></i> 保存 </button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="box box-warning ">
                    <div class="box-header">
                        <h4 class="box-title"><b>现有用户信息传输装置列表</b></h4>
                    </div>
                    <div class="box-body">
                        <table id="eq_table" class="table table-bordered table-striped" style="width: 100%">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>设备名称</th>
                            <th>设备IP</th>
                            <th>设备备注</th>
                            <th>设备状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="eq_table_tbody">

                        </tbody>
                    </table>
                    </div>
                </div>
            </div>
        </section>
        <div class="clearfix"></div>
    </div><!-- /.content-wrapper -->
    <!--内容区结束-->
    <!--页脚开始-->
    <footer class="main-footer no-print">
        <div class="pull-right hidden-xs">
            <b>Version</b>  3.1.4
        </div>
        <strong>Copyright &copy; 2015-2016 <a href="http://www.tpson.cn/" target="_blank">拓深科技</a>.</strong> All rights reserved.
    </footer>
    <!--页脚结束-->
</div>

<script src="assets/plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/layer-v2.2/layer/layer.js"></script>
<script src="assets/AdminLTE/dist/js/app.min.js"></script>
<script src="assets/AdminLTE/dist/js/demo.js"></script>
<!--表格插件-->
<script src="assets/DataTables-1.10.11/media/js/jquery.dataTables.min.js"></script>
<script src="assets/DataTables-1.10.11/media/js/dataTables.bootstrap.min.js"></script>
<!--报警弹窗插件-->
<script src="assets/ipicture/js/jquery.ipicture_location.js"></script>
<script src="js/alert_check.js"></script>
<script>
    var server;
    var monitorTable;
    $(function () {
        var path = document.location.pathname;
        var href = document.location.href;
        if(path =='/'){
            var index = href.indexOf(path,href.indexOf(path)+2);
        }else {
            var index = href.indexOf(path);
        }
        var localPath = href.substring(0,index);
        var localName = path.substring(0,path.substr(1).indexOf('/')+1);
        server = localPath + localName;
        socketInit(server.substring(5));
        initMonitorTable();
        ReadTable();
        window.onload = checkAlert(server);
    });

    function ReadTable() {
        $.ajax({
            url:server+"/xk/xkDevice/getAll" ,
            success:function (data_in) {
                var json = JSON.parse(data_in);
                if(json.length == 0){
                    layer.alert('没有数据！',{
                        closeBtn: 0
                    });
                }else{
                    var monitorDataShow = [];
                    for(var i in json.data){
                        var datas = json.data;
                        var obj = {
                            id: datas[i].id,
                            name:datas[i].name,
                            ip: datas[i].ip,
                            description:datas[i].description,
                            status:datas[i].status,
                        };
                        obj.id=obj.id?obj.id:"无";
                        obj.name=obj.name?obj.name:"无";
                        obj.ip=obj.ip?obj.ip:"无";
                        obj.description=obj.description?obj.description:"无";
                        obj.status=obj.status?obj.status:"无";
                        monitorDataShow.push(obj);
                    }
                    monitorTable.clear().draw();
                    monitorTable.rows.add(monitorDataShow).draw();
                }
//                layer.close(index);
            }
        });
    }

    function initMonitorTable() {
        monitorTable = $('#eq_table').DataTable({
            paging: true,
            columns:[
                {data: "id"},
                {data: "name"},
                {data: "ip"},
                {data: "description"},
                {data: null,"width":"80px"},
                {data: null,"width":"180px"}
            ],
            columnDefs:[{
                targets:4,
                render: function (data, type, row, meta) {
                    var obj2 = encodeURIComponent(JSON.stringify(row));
                    if(row.status=="1"){
                        return '<div class="row"> <div class="col-sm-12"> ' +
                                '<div class="col-sm-5" style="margin-left: 5px" >'+
                                '<button type="button" class="btn btn-success">在线</button></div> </div></div>';
                    }else{
                        return '<div class="row"> <div class="col-sm-12"> ' +
                                '<div class="col-sm-5" style="margin-left: 5px" >'+
                                '<button type="button" class="btn btn-danger">异常</button></div> </div></div>';
                    }
                }
            },{
                targets:5,
                render: function (data, type, row, meta) {
                    var obj2 = encodeURIComponent(JSON.stringify(row));
                    return '<div class="row"> <div class="col-sm-12"> ' +
                            '<div class="col-sm-5" style="margin-left: 5px" >'+
                            '<button type="button" class="btn btn-warning fa fa-edit" onclick='+"editRecord("+'"'+obj2+'"'+")"+ '>编辑</button></div>' +
                            '<div class="col-sm-5" style="margin-left: 5px"><button type="button" class="btn btn-danger fa fa-remove" onclick=delFromID('+row.id+')>删除</button></div> ' +
                            '</div></div>';
                }
            },
                {   'orderable': false,
                    'targets': 5
                },],

            language:{
                "sProcessing":   "处理中...",
                "sLengthMenu":   "显示 _MENU_ 项结果",
                "sZeroRecords":  "没有匹配结果",
                "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix":  "",
                "sSearch":       "搜索:",
                "sUrl":          "",
                "sEmptyTable":     "表中数据为空。",
                "sLoadingRecords": "载入中...",
                "sInfoThousands":  ",",
                "oPaginate": {
                    "sFirst":    "首页",
                    "sPrevious": "上页",
                    "sNext":     "下页",
                    "sLast":     "末页"
                },
                "oAria": {
                    "sSortAscending":  ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            }
        });
    };
    //修改设备
    function editRecord(obj) {
        var data = JSON.parse(decodeURIComponent(obj));
        var id = data.id,
                name=data.name,
                ip=data.ip,
                description=data.description
                status=data.status=="1"?1:0;
        layer.open({
            type: 1,
            title:'修改设备信息,否则请点击“取消”',
            skin:'layui-layer-lan',
            area:['420px', 'auto'],
            content: '<div class="row" style="width: 420px;">'
            +'  <div class="col-sm-12">'
            +'      <div class="input-group">'
            +'          <span class="input-group-addon"><i class="fa fa-sitemap text-yellow"></i></span>'
            +'          <input id="popName" type="text" class="form-control" placeholder="请输入设备名称" value="'+name+'">'
            +'      </div>'
            +'  </div>'
            +'  <div class="col-sm-12" style="margin-top: 10px">'
            +'      <div class="input-group">'
            +'          <span class="input-group-addon"><i class="fa fa-plug text-yellow"></i></span>'
            +'          <input id="popIP" type="text" disabled class="form-control" placeholder="请输入设备IP" value="'+ip+'">'
            +'      </div>'
            +'  </div>'
            +'  <div class="col-sm-12" style="margin-top: 10px">'
            +'      <div class="input-group">'
            +'          <span class="input-group-addon"><i class="fa fa-file-text text-yellow"></i></span>'
            +'          <input id="popDescription" type="text" class="form-control" placeholder="请输入设备备注" value="'+description+'">'
            +'      </div>'
            +'  </div>'
            +'</div>'
            ,
            btn:['保存','取消'],
            btn1: function (index,layero) {
                var pop_name = $('#popName').val();
                var pop_description = $('#popDescription').val();
                var pop_ip = $('#popIP').val();

                var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
                if(re.test(pop_ip)){
                    if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
                        var formDatas = new FormData();
                        formDatas.append("id",id);
                        formDatas.append("name",pop_name);
                        formDatas.append("ip",pop_ip);
                        formDatas.append("description",pop_description);
                        formDatas.append("status",status);
                        $.ajax({
                            type:"POST",
                            contentType : false,
                            processData:false,
                            url:server + "/xk/xkDevice/update",
                            data:formDatas,
                            success:function (data) {
                                layer.alert('数据修改成功！',{
                                    closeBtn: 0
                                });
                                ReadTable();
                            }
                        });
                    }else {
                        layer.alert('IP格式错误！',{
                            closeBtn: 0
                        });
                    }
                }else {
                    layer.alert('IP格式错误！',{
                        closeBtn: 0
                    });
                }

            },
            btn2:function (index,layero) {

            }

        });
    }
    //录入设备
    $('#saveEqBtn').click(function () {
        var name = $('#eqName').val();
        var description = $('#eqDes').val();

        var ip = $('#eqIP').val();
        var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
        if(re.test(ip)){
            if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
                var formDatas = new FormData();
                formDatas.append("name",name);
                formDatas.append("ip",ip);
                formDatas.append("description",description);
                $.ajax({
                    type:"post",
                    contentType : false,
                    processData:false,
                    url:server + "/xk/xkDevice/add",
                    data:formDatas,
                    success:function (data) {
                        layer.alert('数据添加成功！',{
                            closeBtn: 0
                        });
                        ReadTable();
                    }
                });
            }else {
                layer.alert('IP格式错误！',{
                    closeBtn: 0
                });
            }
        }else{
            layer.alert('IP格式错误！',{
                closeBtn: 0
            });
        }


    });
    //删除设备
    function delFromID(id) {
        $.ajax({
            async:false,
            url:server +'/xk/xkDevice/delete?id='+id,
            success:function (data) {
                layer.alert('数据删除成功！',{
                    closeBtn: 0
                });
                ReadTable();
            }
        });
    }
</script>
</body>
</html>